<?php

App::uses('AppController', 'Controller');

class AnalystController extends AppController {

    public $helpers = array('Html');

    public function view($id) {
        $this->loadModel('Broker');
        $this->loadModel('Stock');
        $this->loadModel('StockValues');
        $this->loadModel('Opinion');
        if (!$id) {
            throw new NotFoundException(__('Invalid post'));
        }
        $broker = $this->Broker->findById($id);
        if (!$broker) {
            throw new NotFoundException(__('Invalid post'));
        }

        $this->paginate = array(
            'conditions' => array('status' => 1, 'broker' => $broker['Broker']['id']),
            'limit' => 12,
            'order' => array('id' => 'desc')
        );

        // we are using the 'Admin' model
        $rows = $this->paginate('Stock');
        foreach ($rows as $rk => $stock) {
            $stock_last_value = $this->StockValues->find('all', array('limit' => 2, 'order' => array('date desc', 'updated desc'), 'conditions' => array('stock' => $stock['Stock']['id'])));
            $stock['Stock']['values'] = $stock_last_value;
            $stock_last_opinion = $this->Opinion->find('first', array('order' => 'date desc', 'conditions' => array('stock' => $stock['Stock']['id'], 'broker' => $id, 'result >' => 0)));
            $stock['Stock']['opinion'] = $stock_last_opinion;
            $rows[$rk] = $stock;
        }
        // pass the value to our view.ctp
        $this->set('rows', $rows);
        $this->set('broker', $broker);
    }

}
